package com.atguigu.spzx.cart.controller;

import com.atguigu.spzx.cart.service.CartService;
import com.atguigu.spzx.model.pojo.h5.CartInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.utils.DYLAssert;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/order/cart")
@Tag(name = "购物车管理")
public class CartController {
    @Autowired
    private CartService cartService;


    @GetMapping("/auth/addToCart/{skuId}/{skuNum}")
    @Operation(summary = "添加购物车")
    public Result addToCart(@PathVariable("skuId")Long skuId,
                            @PathVariable("skuNum")Integer skuNum){
        cartService.addToCart(skuId,skuNum);
        return Result.ok();
    }

    @GetMapping("/auth/cartList")
    @Operation(summary = "获取购物车信息")
    public Result<List<CartInfo>> getCartList(){
        return Result.ok(cartService.getCartList());
    }

    @DeleteMapping("/auth/deleteCart/{skuId}")
    @Operation(summary = "删除购物车选项")
    public Result deleteCartItem(@PathVariable("skuId")Integer skuId){
        DYLAssert.isTrue(cartService.deleteCartItem(skuId),"删除失败");
        return Result.ok();
    }

    @GetMapping("/auth/checkCart/{skuId}/{isChecked}")
    @Operation(summary = "更新购物车商品选中状态")
    public Result checkCart(@PathVariable("skuId")Integer skuId,
                            @PathVariable("isChecked")Integer isChecked){
        cartService.checkCart(skuId,isChecked);
        return Result.ok();
    }

    @GetMapping("/auth/allCheckCart/{isChecked}")
    @Operation(summary = "购物车全选")
    public Result checkAllCart(@PathVariable("isChecked")Integer isChecked){
        DYLAssert.isTrue(cartService.checkAllCart(isChecked),"全选失败");
        return Result.ok();
    }

    @GetMapping("/auth/clearCart")
    @Operation(summary = "清空购物车")
    public Result clearCart(){
        cartService.clearCart();
        return Result.ok();
    }

    @GetMapping("/auth/getAllCkecked")
    @Operation(summary = "选中的购物车")
    public Result<List<CartInfo>> getAllChecked(){
        return Result.ok(cartService.getAllChecked());
    }
}
